package com.jay.tools.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jay.tools.R;

import java.lang.ref.WeakReference;


/**
 * 自定义CheckBox<br>
 * 设置checkBox图片是应设置为select属性例：<br>
 * <?xml version="1.0" encoding="utf-8"?> <br>
 * <selector xmlns:android="http://schemas.android.com/apk/res/android"><br>
 * <item android:drawable="@drawable/all_box_icon" android:state_selected="false"/> <br>
 * <item android:drawable="@drawable/all_chekde_icon" android:state_selected="true"/><br>
 * </selector>
 */
public class Cbox extends LinearLayout implements OnClickListener {
    // checkbox大小
    private int mCbSize = 25;
    // 图片距离文字的左边距
    private int mLeftSpace = 1;
    // 图片是否选中
    private boolean mChecked = false;
    // 控件是否可以点击
    private boolean mEnable = true;
    // CheckBox复选框图片
    private ImageView mCheckBox;
    private int mCBoxBack;

    // 图片
    private ImageView mImageView;
    private int mImageRes;
    private int mImgSize = 30;
    // 文字
    private String mText;
    private int mTextSize = sp2px(16);
    private int mTextColor = 0xFFffffff;
    private TextView mTextView;

    private OnCboxClickListener mListener;

    public Cbox(Context context) {
        super(context);
    }

    public Cbox(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTypeArray(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.ui_layout_cbox_view, this, true);
        // 初始化checkbox
        mCheckBox = this.findViewById(R.id.iv_checkbox);
        mCheckBox.setBackgroundResource(mCBoxBack);

        LayoutParams params = new LayoutParams(mCbSize, mCbSize);
        mCheckBox.setLayoutParams(params);
        mCheckBox.setSelected(mChecked);

        // 初始化文字
        if (!TextUtils.isEmpty(mText)) {
            mTextView = this.findViewById(R.id.tv_descrision);
            mTextView.setVisibility(View.VISIBLE);
            // 设置文字左间距
            mTextView.setPadding(mLeftSpace, 0, 0, 0);
            mTextView.setText(mText);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
            mTextView.setTextColor(mTextColor);
        }
        if (-1 != mImageRes) {
            mImageView = this.findViewById(R.id.iv_other_img);
            mImageView.setVisibility(View.VISIBLE);
            mImageView.setImageResource(mImageRes);
            LayoutParams lp = new LayoutParams(mImgSize, mImgSize);
            lp.setMargins(mLeftSpace, 0, 0, 0);
            mImageView.setLayoutParams(lp);
            mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        }
        // 为整个checkbox添加点击事件
        View v = this.findViewById(R.id.my_checkbox_linearLayout);
        v.setOnClickListener(this);
    }

    public void setText(String text) {
        mText = text;
        if (!TextUtils.isEmpty(mText)) {
            mTextView = this.findViewById(R.id.tv_descrision);
            mTextView.setVisibility(View.VISIBLE);
            // 设置文字左间距
            mTextView.setPadding(mLeftSpace, 0, 0, 0);
            mTextView.setText(mText);
            mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
            mTextView.setTextColor(mTextColor);
        }
    }

    private void initTypeArray(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Cbox);
        mChecked = a.getBoolean(R.styleable.Cbox_cbox_checked, false);
        mEnable = a.getBoolean(R.styleable.Cbox_cbox_enable, true);
        mCBoxBack = a.getResourceId(R.styleable.Cbox_cbox_background, R.drawable.selector_check_box);

        mImageRes = a.getResourceId(R.styleable.Cbox_cbox_img, -1);
        mText = a.getString(R.styleable.Cbox_cbox_text);
        mTextColor = a.getColor(R.styleable.Cbox_cbox_text_color, mTextColor);

        mImgSize = a.getDimensionPixelSize(R.styleable.Cbox_cbox_img_size, mImgSize);
        mLeftSpace = a.getDimensionPixelSize(R.styleable.Cbox_cbox_left_width, mLeftSpace);
        mCbSize = a.getDimensionPixelSize(R.styleable.Cbox_cbox_size, mCbSize);
        mTextSize = a.getDimensionPixelSize(R.styleable.Cbox_cbox_text_size, mTextSize);
        a.recycle();
    }

    @Override
    public void onClick(View v) {
        if (!mEnable) {
            return;
        }
        mCheckBox.setSelected(!mChecked);
        mChecked = !mChecked;
        if (null != mListener) {
            mListener.onCboxClick(Cbox.this);
        }
    }

    private int sp2px(int dp) {
        return (int) (dp * getContext().getResources().getDisplayMetrics().scaledDensity + 0.5);
    }

    public void setOnCboxClickListener(OnCboxClickListener listener) {
        mListener = listener;
    }

    public interface OnCboxClickListener {
        void onCboxClick(View v);
    }

    public void setChecked(boolean isAllCheck) {
        mCheckBox.setSelected(isAllCheck);
    }

    public boolean isChecked() {
        mChecked = mCheckBox.isSelected();
        return mChecked;
    }

    public String getText() {
        return mText;
    }

}
